﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace ClientLauncher
{
    public partial class LauncherView : Form
    {
        List<Process> _processes;

        public LauncherView()
        {
            InitializeComponent();
            _processes = new List<Process>();

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.Left = Properties.Settings.Default.WindowLeft;
            this.Top  = Properties.Settings.Default.WindowTop;
            uxClientCount.Text = Properties.Settings.Default.ClientCount.ToString();
            uxStorage.Text = Properties.Settings.Default.StoragePath;
            uxNodePrefix.Text = Properties.Settings.Default.NodePrefix;
        }
        private void uxStart_Click(object sender, EventArgs e)
        {
            Process p;
            int count = Int32.Parse(uxClientCount.Text);
            for (int i = 0; i < count; i++)
            {
                p = new Process();
                p.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
                p.StartInfo.FileName = "ClientPerformance.exe";
                p.StartInfo.Arguments = uxNodePrefix.Text + "_" + i.ToString();

                p.Exited += new EventHandler(p_Exited);
                p.EnableRaisingEvents = true;

                // Store the processes we created
                _processes.Add(p);
                p.Start();

                // Let each process spin up and join the mesh 
                Thread.Sleep(1000);
            }
        }

        void p_Exited(object sender, EventArgs e)
        {
            _processes.Remove(sender as Process);
        }

        private void uxStop_Click(object sender, EventArgs e)
        {
            foreach (var item in _processes)
            {
                item.CloseMainWindow();
            }
            Thread.Sleep(1000);


            string newFolderName = DateTime.Now.ToString("MM-dd-yyyy_hh-mm");
            string newFolderPath = uxStorage.Text + "\\" + newFolderName;

            string[] files = Directory.GetFiles(Environment.CurrentDirectory, "*.txt");
            if (files.Length > 0)
            {
                if (!Directory.Exists(newFolderPath))
                {
                    Directory.CreateDirectory(newFolderPath);
                    Thread.Sleep(500);
                }

                foreach (var file in files)
                {
                    FileInfo info = new FileInfo(file);
                    
                    string newFilePath = newFolderPath + "\\" + info.Name;
                    File.Move(info.FullName, newFilePath);
                }
            }
        }

        private void LauncherView_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.WindowLeft = this.Left;
            Properties.Settings.Default.WindowTop = this.Top;
            Properties.Settings.Default.ClientCount = Int32.Parse(uxClientCount.Text);
            Properties.Settings.Default.StoragePath = uxStorage.Text;
            Properties.Settings.Default.NodePrefix = uxNodePrefix.Text;
            Properties.Settings.Default.Save();
        }
    }
}
